package com.zhentao.interceptor;


import com.zhentao.model.common.app.pojo.AppUser;
import com.zhentao.util.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class AppTokenInterceptor implements HandlerInterceptor {


    //在方法执行之前调用   true放行  false拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取到header里面的信息
        String userId=request.getHeader("userId");
        if(userId!=null){
            //把userId放到threadLocal里面
            AppUser appUser=new AppUser();
            appUser.setId(Integer.valueOf(userId));
            AppThreadLocalUtil.setUser(appUser);

            log.info("把用户信息放到了ThreadLocal里面，{}",appUser);

        }

        return true;


    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
